﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI.WebControls;

namespace Ordina.SharePoint.FacebookLike.Controls
{

    public class FacebookLike : Label
    {
        private string _url;
        public string Url 
        { 
            get { return _url; } 
            set { _url = value; } 
        }
        private bool _sendButton;
        public bool SendButton 
        { 
            get { return _sendButton; } 
            set { _sendButton = value; } 
        }
        private bool _showFaces;
        public bool ShowFaces 
        { 
            get { return _showFaces; } 
            set { _showFaces = value; } 
        }
        private ColorScheme _color;
        public ColorScheme Color 
        {
            get { return _color; } 
            set { _color = value; } 
        }
        private LayoutStyle _layout;
        public LayoutStyle Layout 
        { 
            get { return _layout; } 
            set { _layout = value; } 
        }
        private VerbToDisplay _verb;
        public VerbToDisplay Verb 
        { 
            get { return _verb; } 
            set { _verb = value; } 
        }

        //TODO HTML 5 - Bug Javascript error...
        //protected override void Render(System.Web.UI.HtmlTextWriter writer)
        //{
        //    writer.Write("<div id=\"fb-root\"></div>");
        //    writer.Write("<script>(function(d, s, id) {");
        //    writer.Write(" var js, fjs = d.getElementsByTagName(s)[0];");
        //    writer.Write("if (d.getElementById(id)) return;");
        //    writer.Write("js = d.createElement(s); js.id = id;");
        //    writer.Write("js.src = \"//connect.facebook.net/en_US/all.js#xfbml=1\";");
        //    writer.Write("fjs.parentNode.insertBefore(js, fjs);");
        //    writer.Write("}(document, 'script', 'facebook-jssdk'));</script>");

        //    string layout = "standard";
        //    if (Layout == FacebookLike.LayoutStyle.box_count)
        //        layout = "box_count";
        //    if (Layout == FacebookLike.LayoutStyle.button_count)
        //        layout = "box_count";

        //    string color = "light";
        //    if (Color == FacebookLike.ColorScheme.dark)
        //        color = "dark";

        //    string ver = string.Empty;
        //    if (Verb == FacebookLike.VerbToDisplay.recommend)
        //        ver = "data-action=\"recommend\" ";

        //    writer.Write("<div class=\"fb-like\" data-layoutstyle=\"" + layout + "\" data-href=\"" + Url + "\" data-send=\"" + SendButton.ToString().ToLower() + "\" data-width=\"" + Width + "\" data-show-faces=\"" + ShowFaces.ToString().ToLower() + "\" " + ver + "data-font=\"lucida grande\" data-colorscheme=\"" + color + "\"></div>");
        //}

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(_url))
                _url = this.Context.Request.Url.AbsoluteUri;

            string send = _sendButton.ToString().ToLower();
            string showFaces = _showFaces.ToString().ToLower();

            string layout = "standard";
            if (_layout == LayoutStyle.box_count)
                layout = "box_count";
            if (_layout == LayoutStyle.button_count)
                layout = "box_count";

            string color = "light";
            if (_color == ColorScheme.dark)
                color = "dark";

            string ver = string.Empty;
            if (_verb == VerbToDisplay.recommend)
                ver = "data-action=\"recommend\" ";
            string allowTransparency = "allowtransparency";
            string css = "border-bottom: medium none; border-left: medium none; overflow: hidden; border-top: medium none; border-right: medium none;";

            writer.Write("<iframe src=\"//www.facebook.com/plugins/like.php?" +
                "href={0}&" +
                "send={1}&" +
                "layout={2}&" +
                "width={3}&" +
                "show_faces={4}&" +
                "action={5}&" +
                "colorscheme={6}&" +
                "font&" +
                "height={7}\" " +
                "frameBorder=\"0\" " +
                "scrolling=\"no\" " +
                "style=\"width: {3}px; height: {7}px; {8}\" " +
                "allowTransparency=\"{9}\"></iframe>"
                , HttpUtility.UrlEncode(_url), send, layout, Width, showFaces, ver, color, Height,css, allowTransparency);
        }
    }
}

